package com.mach.platform.api.util;

import cn.hutool.setting.dialect.Props;
import com.alibaba.fastjson.JSONObject;
import com.mach.platform.api.enums.MessageInfo;
import com.mach.platform.domain.Response;
import com.zhenzi.sms.ZhenziSmsClient;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang3.RandomStringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class AppletsUtil {

    /**
     * 微信小程序的APP_ID
     */
    private static final String APPLETS_APP_ID;

    /**
     * 微信小程序的app_Secret
     */
    private static final String APPLETS_APP_SECRET;

    /**
     * 短信验证码发送平台APP_ID
     */
    private static final String SMS_APP_ID;

    /**
     * 短信验证码发送平台SMS_APP_SECRET
     */
    private static final String SMS_APP_SECRET;

    /**
     * 短信验证码平台请求地址
     */
    private static final String url = "https://sms_developer.zhenzikj.com";


    /**
     * 初始化小程序配置信息
     */
    static {
        Props props = new Props("applets.properties");
        APPLETS_APP_ID = props.getStr("applets.appId");
        APPLETS_APP_SECRET = props.getStr("applets.appSecret");
        SMS_APP_ID = props.getStr("sms.appId");
        SMS_APP_SECRET = props.getStr("sms.appSecret");
    }


    /**
     * 请求微信接口，获取微信用户openId和sessionKey信息
     * @param code
     * @return
     */
    public static Response getAppletsUserInfo(String code) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+APPLETS_APP_ID+"&secret="+APPLETS_APP_SECRET+"&js_code="+code+"&grant_type=authorization_code";
        try {
            // 创建httpClient实例对象
            HttpClient httpClient = new HttpClient();
            // 设置httpClient连接主机服务器超时时间
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
            // 创建GET请求方法实例对象
            GetMethod getMethod = new GetMethod(url);
            int status = httpClient.executeMethod(getMethod);
            if (status == 200) {
                String result = getMethod.getResponseBodyAsString();
                JSONObject object = JSONObject.parseObject(result);
                getMethod.releaseConnection();
                int errCode = object.getIntValue("errcode");
                if (errCode != 0) {
                    return Response.error(MessageInfo.getMessage(errCode));
                }
                Map map = new HashMap();
                map.put("openId", object.getString("openid"));
                map.put("sessionKey", object.getString("session_key"));
                return Response.ok(map);
            } else {
                return Response.error("获取微信用户信息异常！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Response.error("请求微信接口异常！");

    }


    /**
     * 发送手机验证码的短信
     * @param phone
     * @return
     */
    public static Map sendSms(String phone) {
        String verifyCode = RandomStringUtils.randomNumeric(4);
        ZhenziSmsClient client = new ZhenziSmsClient(url, SMS_APP_ID, SMS_APP_SECRET);
        try {
            String content = "验证码为" + verifyCode + "，5分钟内有效，请勿泄漏。如非本人操作，请忽略本条消息。";
            String result = client.send(phone, content);
            JSONObject json = JSONObject.parseObject(result);
            Map map = new HashMap<String, Object>();
            map.put("code", json.getIntValue("code"));
            map.put("verifyCode", verifyCode);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



}
